package com.iot.comprehensive.face.controller;

import com.iot.comprehensive.face.dto.RecordInfoDto;
import com.iot.comprehensive.face.model.Account;
import com.iot.comprehensive.face.model.Record;
import com.iot.comprehensive.face.model.User;
import com.iot.comprehensive.face.service.AccountService;
import com.iot.comprehensive.face.service.RecordService;
import com.iot.comprehensive.face.service.UserService;
import com.iot.comprehensive.face.utils.Jwt;
import com.iot.comprehensive.face.utils.Result;
import com.iot.comprehensive.face.utils.ResultCode;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 朱泽玉
 * @Create 2021/7/4 20:41
 * @Description
 * @Modified By:
 */
@RestController
@RequestMapping("/record")
@Api(tags = "记录查看", protocols = "http,https")
@ApiResponses({
        @ApiResponse(code = 200, message = "登录成功"),
        @ApiResponse(code = 300, message = "出现错误"),
        @ApiResponse(code = 301, message = "无权限(未认证)")
})
public class RecordController {
    final com.iot.comprehensive.face.utils.Jwt jwt;
    final UserService userService;
    final RecordService recordService;
    final AccountService accountService;
    public RecordController(Jwt jwt, UserService userService, RecordService recordService, AccountService accountService) {
        this.jwt = jwt;
        this.userService = userService;
        this.recordService = recordService;
        this.accountService = accountService;
    }
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部记录", notes = "获取全部记录")
    public Result getAll(HttpServletRequest request) throws IOException {
        // 1、获取返回的token
        Claims token = jwt.token(request);
        // 2、判断是否携带token
        if (token == null) {
            return new Result(ResultCode.FAIL, "请重新登录后再次尝试");
        }
        // 3、获取token中的id值
        Long id = Long.valueOf(token.getId());
        Account account = accountService.findById(id);
        if (account.getIdentity().equals("host") || account.getIdentity().equals("user")){
            List<Record> recordList = recordService.findByAccount(id);
            return new Result(ResultCode.SUCCESS,recordList);
        }
        if (account.getIdentity().equals("admin")){
            List<Record> recordList = recordService.findAll();
            List<RecordInfoDto> recordInfoList = new ArrayList<>();
            for(Record record : recordList){
                // 获取开门人信息
                Long user_id = record.getAccount();
                User user = userService.getByAccount(user_id);
                Account user_account = accountService.findById(user_id);
                RecordInfoDto recordInfoDto = new RecordInfoDto();
                recordInfoDto.setUser(user);
                recordInfoDto.setAccount(user_account);
                recordInfoDto.setRecord(record);
                assert false;
                recordInfoList.add(recordInfoDto);
            }
            return new Result(ResultCode.SUCCESS,recordInfoList);
        }
        // 4、获取个人信息
        return new Result(ResultCode.FAIL);
    }
}
